package br;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class pozycja
 */
@WebServlet("/pozycja")
public class pozycja extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public pozycja() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession sesja = request.getSession(true);
		 response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		System.out.println(sesja.getAttribute("log")+" "+sesja.getAttribute("nick"));
		if(sesja!=null&&sesja.getAttribute("log")!=null&&sesja.getAttribute("log").equals("pracownik")){ 
		 String nick = (String)sesja.getAttribute("nick");
		 System.out.println(nick);
		 String odp="";
			try {
				Connection con2 =mb.commons.DbConnection.getConnection();
				  Statement stmt2 = con2.createStatement();
				  ResultSet rs2 = stmt2.executeQuery("SELECT pracownicy.id_oddzialu FROM pracownicy,uzytkownicy WHERE pracownicy.id=uzytkownicy.id AND uzytkownicy.login='"+nick+"'");
				String id="0";
				  while(rs2.next())
					id=rs2.getString("pracownicy.id_oddzialu");
			  Connection con =mb.commons.DbConnection.getConnection();
			  Statement stmt = con.createStatement();
			  ResultSet rs = stmt.executeQuery("SELECT * FROM kurierzy_pozycja,kurierzy WHERE kurierzy.id_kuriera=kurierzy_pozycja.id_kuriera AND kurierzy.id_oddzialu="+id);
			  Calendar cal = Calendar.getInstance();
			  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			  
			  int i =0;
			  while(rs.next()){
				 odp+=""+rs.getDouble("lat")+","+rs.getDouble("lng")+","+rs.getString("kurierzy.imie")+","+rs.getString("kurierzy.nazwisko")+","+rs.getString("ostatniapoz")+","+sdf.format(cal.getTime())+"|";
				 i++;
			  }	  
			  System.out.println(odp);
			  out.println(odp);
			  
			} catch (Exception e) {
			}
		
	}
		if(sesja!=null&&sesja.getAttribute("log")!=null&&sesja.getAttribute("log").equals("admin")){ 
			 String nick = (String)sesja.getAttribute("nick");
			 System.out.println(nick);
			 String odp="";
				try {
				//	Connection con2 =mb.commons.DbConnection.getConnection();
			//	  Statement stmt2 = con2.createStatement();
		//			  ResultSet rs2 = stmt2.executeQuery("SELECT pracownicy.id_oddzialu FROM pracownicy,uzytkownicy WHERE pracownicy.id=uzytkownicy.id AND uzytkownicy.login='"+nick+"'");
			//		String id="0";
			//		  while(rs2.next())
			//			id=rs2.getString("pracownicy.id_oddzialu");
				  Connection con =mb.commons.DbConnection.getConnection();
				  Statement stmt = con.createStatement();
				  ResultSet rs = stmt.executeQuery("SELECT * FROM kurierzy_pozycja,kurierzy WHERE kurierzy.id_kuriera=kurierzy_pozycja.id_kuriera");
				  Calendar cal = Calendar.getInstance();
				  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				  
				  int i =0;
				  while(rs.next()){
					 odp+=""+rs.getDouble("lat")+","+rs.getDouble("lng")+","+rs.getString("kurierzy.imie")+","+rs.getString("kurierzy.nazwisko")+","+rs.getString("ostatniapoz")+","+sdf.format(cal.getTime())+"|";
					 i++;
				  }	  
				  System.out.println(odp);
				  out.println(odp);
				  
				} catch (Exception e) {
				}
			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
